<template>
  <el-container v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" label-position="left" size="small">
      <div>
        <h4>阿里云短信</h4>
        <hr>
      </div>
      <el-form-item label="阿里云accessKeyId" prop="access_key_id">
        <el-input v-model="ruleForm.access_key_id" placeholder="请输入阿里云accessKeyId"></el-input>
      </el-form-item>

      <el-form-item label="阿里云accessKeySecret" prop="access_key_secret">
        <el-input v-model="ruleForm.access_key_secret" placeholder="请输入阿里云accessKeySecret"></el-input>
      </el-form-item>

      <el-form-item label="短信签名" prop="sign">
        <el-input v-model="ruleForm.sign" placeholder="请输入短信签名"></el-input>
      </el-form-item>

      <el-form-item label="注册验证码">
        <el-form-item label="模板名称"  label-width="100px">
          <el-input v-model="ruleForm.reg_tpl.name" placeholder="请输入模板名称"></el-input>
        </el-form-item>
        <el-form-item label="模板ID"  label-width="100px">
          <el-input v-model="ruleForm.reg_tpl.tpl" placeholder="请输入模板ID"></el-input>
        </el-form-item>
        <el-form-item label="模板变量备注"  label-width="100px">
          <el-input v-model="ruleForm.reg_tpl.msg" placeholder="变量如$msg设置成msg如$code设置成code"></el-input>
          <div class="form-item-tip">
            <span>提示：变量如$msg设置成msg如$code设置成code</span>
          </div>
        </el-form-item>
      </el-form-item>

      <el-form-item label="收货验证码">
        <el-form-item label="模板名称"  label-width="100px">
          <el-input v-model="ruleForm.collect_tpl.name" placeholder="请输入模板名称"></el-input>
        </el-form-item>
        <el-form-item label="模板ID"  label-width="100px">
          <el-input v-model="ruleForm.collect_tpl.tpl" placeholder="请输入模板ID"></el-input>
        </el-form-item>
        <el-form-item label="模板变量备注"  label-width="100px">
          <el-input v-model="ruleForm.collect_tpl.msg" placeholder="变量如$msg设置成msg如$code设置成code"></el-input>
          <div class="form-item-tip">
            <span>提示：变量如$msg设置成msg如$code设置成code</span>
          </div>
        </el-form-item>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即保存</el-button>
        <!--<el-button @click="resetForm('ruleForm')">重置</el-button>-->
      </el-form-item>
    </el-form>

  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        loading: true,  // 页面加载
        ruleForm: {
          access_key_id:"",                //类型：String  必有字段  备注：阿里云accessKeyId
          access_key_secret:"",                //类型：String  必有字段  备注：阿里云accessKeySecret
          sign:"",                //类型：String  必有字段  备注：短信签名
          reg_tpl:{                //类型：Object  必有字段  备注：注册验证码
            name:"",                //类型：String  必有字段  备注：模板名称
            tpl:"",                //类型：String  必有字段  备注：模板ID
            msg:""                //类型：String  必有字段  备注：模板变量备注(变量如$msg设置成msg如$code设置成code)
          },
          collect_tpl:{                //类型：Object  必有字段  备注：收货验证码
            name:"",                //类型：String  必有字段  备注：无
            tpl:"",                //类型：String  必有字段  备注：无
            msg:""                //类型：String  必有字段  备注：无
          },
          id:"4"                //类型：String  必有字段  备注：无
        },
        rules: {
          access_key_id: [
            {required: true, message: '请输入阿里云accessKeyId', trigger: 'blur'},
          ],
          access_key_secret: [
            {required: true, message: '请输入阿里云accessKeySecret', trigger: 'blur'},
          ],
          sign: [
            {required: true, message: '请输入短信签名', trigger: 'blur'},
          ],
        },
      };
    },
    methods: {
      submitForm(formName) {
        let _this = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            _this.api.updateSmsSetting(_this.ruleForm).then(function (res) {
              if (res.code == 1) {
                _this.$message({
                  showClose: true,
                  message: res.msg,
                  type: 'success'
                });
              } else {
                _this.$message({
                  showClose: true,
                  message: res.msg,
                  type: 'warning'
                });
              }
            }).catch(function (error) {
              throw new Error(error);
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },

    mounted: function () {
      let _this = this;
      this.api.getSmsSetting().then(function (res) {
        if (res.code == 1) {
          _this.ruleForm = res.data;
        }
        _this.loading = false;
      }).catch(function (res) {
        throw new Error(error);
      });
    },
  }
</script>

<style scoped>

  .el-container {
    display: block;
  }

  /* 表单 */
  .el-form {
    width: 1000px;
    margin: 15px 0 15px 30px;
  }

  /* 图片上传 */
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    width: 178px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
